<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="介绍人" prop="memberPid">
        <el-input
          v-model="queryParams.memberPid"
          placeholder="请输入介绍人(上级)ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="用户账号" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="memberNickName">
        <el-input
          v-model="queryParams.memberNickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户邮箱" prop="memberEmail">
        <el-input
          v-model="queryParams.memberEmail"
          placeholder="请输入用户邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="memberPhone">
        <el-input
          v-model="queryParams.memberPhone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="头像地址" prop="numberAvatar">
        <el-input
          v-model="queryParams.numberAvatar"
          placeholder="请输入头像地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="numberPassword">
        <el-input
          v-model="queryParams.numberPassword"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['center:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['center:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['center:member:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['center:member:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户账号" align="center" prop="memberName" />
      <el-table-column label="用户昵称" align="center" prop="memberNickName" />
      <el-table-column label="介绍码" align="center" prop="memberPidCode" />
      <el-table-column label="用户类型" align="center" prop="memberType" />
      <el-table-column label="用户邮箱" align="center" prop="memberEmail" />
      <el-table-column label="手机号码" align="center" prop="memberPhone" />
      <el-table-column label="用户性别" align="center" prop="numberSex" />
      <el-table-column label="头像地址" align="center" prop="numberAvatar" />
      <el-table-column label="帐号状态" align="center" prop="numberStatus" />
      <el-table-column label="介绍人" align="center" prop="memberPid" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['center:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['center:member:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="介绍码" prop="memberPidCode">
          <el-input v-model="form.memberPidCode" placeholder="请输入介绍码" />
        </el-form-item>
        <el-form-item label="用户账号" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="memberNickName">
          <el-input v-model="form.memberNickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="memberEmail">
          <el-input v-model="form.memberEmail" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="memberPhone">
          <el-input v-model="form.memberPhone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="头像地址" prop="numberAvatar">
          <el-input v-model="form.numberAvatar" placeholder="请输入头像地址" />
        </el-form-item>
        <el-form-item label="密码" prop="numberPassword">
          <el-input v-model="form.numberPassword" placeholder="请输入密码" />
        </el-form-item>
       
        <el-form-item label="姓名" prop="mbExpName">
          <el-input v-model="form.drpMemberExpOne.mbExpName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="mbExpIdNumber">
          <el-input v-model="form.drpMemberExpOne.mbExpIdNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="身份证正反照片" prop="mbExpIdImage">
          <image-upload v-model="form.drpMemberExpOne.mbExpIdImage" :limit="2" />
        </el-form-item>
        <el-form-item label="单位" prop="mbExpUnit">
          <image-upload v-model="form.drpMemberExpOne.mbExpUnit" :limit="2" />
        </el-form-item>
        

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">用户扩展属性信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDrpMemberExp">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDrpMemberExp">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="drpMemberExpList" :row-class-name="rowDrpMemberExpIndex" @selection-change="handleDrpMemberExpSelectionChange" ref="drpMemberExp">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="单位" prop="mbExpUnit" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbExpUnit" placeholder="请输入单位" />
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="mbExpName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbExpName" placeholder="请输入姓名" />
            </template>
          </el-table-column>
          <el-table-column label="是否实名" prop="mbExpVerified" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbExpVerified" placeholder="请输入是否实名" />
            </template>
          </el-table-column>
          <el-table-column label="身份证号" prop="mbExpIdNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbExpIdNumber" placeholder="请输入身份证号" />
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mbExpPhoneNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbExpPhoneNumber" placeholder="请输入手机号" />
            </template>
          </el-table-column>
          <el-table-column label="总业绩" prop="mbExpPerformance" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbExpPerformance" placeholder="请输入总业绩" />
            </template>
          </el-table-column>
          <el-table-column label="积分贡献值" prop="mbExpContribution" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbExpContribution" placeholder="请输入积分贡献值" />
            </template>
          </el-table-column>
          <el-table-column label="个人分享码" prop="mbExpShareCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbExpShareCode" placeholder="请输入个人分享码" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember } from "@/api/center/member";

export default {
  name: "Member",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDrpMemberExp: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员信息管理表格数据
      memberList: [],
      // 用户扩展属性表格数据
      drpMemberExpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberPid: null,
        memberPidCode: null,
        memberName: null,
        memberNickName: null,
        memberType: null,
        memberEmail: null,
        memberPhone: null,
        numberSex: null,
        numberAvatar: null,
        numberPassword: null,
        numberStatus: null,
        numberDelFlag: null,
        numberLoginIp: null,
        numberLoginDate: null,
        createAt: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberPid: [
          { required: true, message: "介绍人(上级)ID不能为空", trigger: "blur" }
        ],
        memberPidCode: [
          { required: true, message: "介绍码不能为空", trigger: "blur" }
        ],
        memberName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        memberNickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员信息管理列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        memberPid: null,
        memberPidCode: null,
        memberName: null,
        memberNickName: null,
        memberType: null,
        memberEmail: null,
        memberPhone: null,
        numberSex: null,
        numberAvatar: null,
        numberPassword: null,
        numberStatus: null,
        numberDelFlag: null,
        numberLoginIp: null,
        numberLoginDate: null,
        createAt: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.drpMemberExpList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getMember(userId).then(response => {
        this.form = response.data;
        this.drpMemberExpList = response.data.drpMemberExpList;
        this.open = true;
        this.title = "修改会员信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.drpMemberExpList = this.drpMemberExpList;
          if (this.form.userId != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除会员信息管理编号为"' + userIds + '"的数据项？').then(function() {
        return delMember(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 用户扩展属性序号 */
    rowDrpMemberExpIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 用户扩展属性添加按钮操作 */
    handleAddDrpMemberExp() {
      let obj = {};
      obj.mbExpUnit = "";
      obj.mbExpName = "";
      obj.mbExpVerified = "";
      obj.mbExpIdNumber = "";
      obj.mbExpPhoneNumber = "";
      obj.mbExpIdImage = "";
      obj.mbExpPerformance = "";
      obj.mbExpContribution = "";
      obj.mbExpShareCode = "";
      this.drpMemberExpList.push(obj);
    },
    /** 用户扩展属性删除按钮操作 */
    handleDeleteDrpMemberExp() {
      if (this.checkedDrpMemberExp.length == 0) {
        this.$modal.msgError("请先选择要删除的用户扩展属性数据");
      } else {
        const drpMemberExpList = this.drpMemberExpList;
        const checkedDrpMemberExp = this.checkedDrpMemberExp;
        this.drpMemberExpList = drpMemberExpList.filter(function(item) {
          return checkedDrpMemberExp.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDrpMemberExpSelectionChange(selection) {
      this.checkedDrpMemberExp = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('center/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
